using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

// 点赞数据控制器
namespace ApiProject.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class PraiseController : ControllerBase
    {
        // 文章表
        private IRepository<Articles> _articlesRepository;
        //点赞
        private IRepository<Praises> _praiseRepository;
        public PraiseController(

            IRepository<Articles> articlesRepository,
            IRepository<Praises> praiseRepository
        )
        {
            _articlesRepository = articlesRepository;
            _praiseRepository = praiseRepository;
        }

        //点赞+1并且点赞表数据+1,点赞总量+1
        [HttpPost]
        public dynamic AddPraise(NewPraise model)
        {
            //接收用户ID和文章Id
            var articleId = model.ArticleId;
            var userId = model.UserId;
            
            Console.WriteLine(articleId);
            Console.WriteLine(userId);
            //通过文章Id找到该篇文章
            var article = _articlesRepository.GetById(articleId);
            //调用方法点赞总量加一
            article.SumPraises += 1;
            _articlesRepository.Update(article);


            //添加点赞表数据
            var praise =
                new Praises
                {
                    ArticleId = articleId,
                    UserId = userId
                };

            _praiseRepository.Insert(praise);

            //返回文章列表
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = praise,
                Msg = "操作成功"
            });

        }

        //取消点赞-1并且点赞表数据-1,点赞总量-1
        [HttpPost, Route("cancelPraise")]
        public dynamic DeletePraise(NewPraise model)
        {
            //接收用户ID和文章Id
            var articleId = model.ArticleId;
            var userId = model.UserId;


            Console.WriteLine(articleId);
            Console.WriteLine(userId);
            //通过文章Id找到该篇文章
            var article = _articlesRepository.GetById(articleId);
            article.SumPraises -= 1;
            _articlesRepository.Update(article);

            //通过文章id和用户Id找到点赞id
            var x = _praiseRepository.Table.Where(x => x.UserId == userId && x.ArticleId == articleId).FirstOrDefault();
            if (x != null)
            {
                _praiseRepository.Delete(x.Id);
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "暂无数据"
                });
            }


            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = article,
                Msg = "操作成功"
            });
        }


        //查看文章Id和用户Id是否在点赞表中
        [HttpPut]
        public dynamic SelectPraise(NewPraise model)
        {
            //接收用户ID和文章Id
            var articleId = model.ArticleId;
            var userId = model.UserId;
            var x = _praiseRepository.Table.Where(x => x.UserId == userId && x.ArticleId == articleId).FirstOrDefault();
            // if (x != null)
            // {
            Console.WriteLine(articleId);
            Console.WriteLine(userId);
            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = x,
                Msg = "该用户已经点赞"
            });
            // }
            // else
            // {
            //     return JsonHelper.Serialize(new
            //     {
            //         Code = 1000,
            //         Data = "",
            //         Msg = "暂无数据,该用户未点赞"
            //     });
            // }


        }
    }
}